import Vue from 'vue'
import component from './component.vue'

let ToastConstructor = Vue.extend(component);

let instance = null;

const Toast = function (options = {}) {

    if (instance !== null) {
        instance.close();
    };

    if (typeof options === "string") {
        options = {
            title: options
        }
    }

    let userOnClose = options.onClose;

    options.onClose = function () {
        instance = null;
        if (typeof userOnClose === 'function') {
            userOnClose();
        }
    };

    instance = new ToastConstructor({
        data: options
    });

    instance.$mount();
    document.body.appendChild(instance.$el);
    instance.show = true;

    return instance;
}

export default Toast